<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\CompanyPrayerLazhu as CompanyPrayerLazhuModel;
use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;
use app\common\model\Company as CompanyModel;

use think\Db;
/**
 * 寺院祈福蜡烛控制器
 * Class Companyprayerlazhu
 * @package app\home\controller
 */
class Companyprayerlazhu extends Base
{
    private $moren = [
        'name' => '祈福蜡烛',
        'intro' => '弟子愿向菩萨诚心祈祷，惟愿',
        'days' => '1,3,5,7,30,90,180,360',
        'standard' =>array(
            [
                'name' => '求平安',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E5%B9%B3%E5%AE%89%E5%8A%A8%E6%95%88.gif',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E6%9C%AA%E7%82%B9%E5%B9%B3%E5%AE%89.png'
                ],
                'price' => '0'
            ],[
                'name' => '求健康',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E5%81%A5%E5%BA%B7%E5%8A%A8%E6%95%88.gif',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E6%9C%AA%E7%82%B9%E5%81%A5%E5%BA%B7.png'
                ],
                'price' => '3'
            ],[
                'name' => '求财运',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E8%B4%A2%E8%BF%90%E5%8A%A8%E6%95%88.gif',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E6%9C%AA%E7%82%B9%E8%B4%A2%E8%BF%90.png'
                ],
                'price' => '3'
            ],[
                'name' => '延寿',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E5%BB%B6%E5%AF%BF%E5%8A%A8%E6%95%88.gif',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E6%9C%AA%E7%82%B9%E5%BB%B6%E5%AF%BF.png'
                ],
                'price' => '6'
            ],[
                'name' => '消灾',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E6%B6%88%E7%81%BE%E5%8A%A8%E6%95%88.gif',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E6%9C%AA%E7%82%B9%E6%B6%88%E7%81%BE.png'
                ],
                'price' => '9'
            ]
        )
    ];
    private $moren1 = [
        'name' => '祈福蜡烛',
        'intro' => '弟子愿向菩萨诚心祈祷，惟愿',
        'days' => '1,3,5,7,30,90,180,360',
        'standard' =>array(
            [
                'name' => '求平安',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E5%B0%8F_%E6%B1%82%E5%B9%B3%E5%AE%89_%E7%82%B9%E8%9C%A1%E7%83%9B.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E5%B0%8F_%E6%B1%82%E5%B9%B3%E5%AE%89_%E6%9C%AA%E7%82%B9%E7%83%9B.png'
                ],
                'price' => '0'
            ],[
                'name' => '求健康',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E5%B0%8F_%E6%B1%82%E5%81%A5%E5%BA%B7_%E7%82%B9%E8%9C%A1%E7%83%9B.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E5%B0%8F_%E6%B1%82%E5%81%A5%E5%BA%B7_%E6%9C%AA%E7%82%B9%E7%83%9B.png'
                ],
                'price' => '3'
            ],[
                'name' => '求财运',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E5%B0%8F_%E6%B1%82%E8%B4%A2%E8%BF%90_%E7%82%B9%E8%9C%A1%E7%83%9B.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E5%B0%8F_%E6%B1%82%E8%B4%A2%E8%BF%90_%E6%9C%AA%E7%82%B9%E7%83%9B.png'
                ],
                'price' => '3'
            ],[
                'name' => '延寿',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E5%B0%8F_%E6%B1%82%E5%BB%B6%E5%AF%BF_%E7%82%B9%E8%9C%A1%E7%83%9B.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E5%B0%8F_%E6%B1%82%E5%BB%B6%E5%AF%BF_%E6%9C%AA%E7%82%B9%E7%83%9B.png'
                ],
                'price' => '6'
            ],[
                'name' => '消灾',
                'img' => [
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E5%B0%8F_%E6%B1%82%E6%B6%88%E7%81%BE_%E7%82%B9%E8%9C%A1%E7%83%9B.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E6%96%B0%E7%A5%88%E7%A6%8F%E8%9C%A1%E7%83%9B/%E5%B0%8F_%E6%B1%82%E6%B6%88%E7%81%BE_%E6%9C%AA%E7%82%B9%E7%83%9B.png'
                ],
                'price' => '9'
            ]
        )
    ];
    /**
     * [getprice  获取价格]
     * @param
     * @author [C。]
     * @return json
     */
    public function getprice()
    {
        $param = input('param.');
        $model = new CompanyPrayerLazhuModel;
        $cmodel = new CompanyModel;
        try{
            $data = $model->findData($param['id'],'comid');
            if(!empty($data)){
                $data['standard'] = unserialize($data['standard']);
            }else{
                $data = $this->moren;
            }
            $company = $cmodel->findData($param['id'],'id','mch_id,much_secret,wx_appid,wx_secret,is_son');
            if($company['is_son'] == 1){
                if(empty($company['mch_id']) || empty($company['wx_appid'])){
                    return admin_json(2, '暂未开通');
                }
            }else{
                if(empty($company['wx_appid']) || empty($company['wx_secret']) || empty($company['mch_id']) || empty($company['much_secret'])){
                    return admin_json(2, '暂未开通');
                }
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [index  获取蜡烛]
     * @param
     * @author [C。]
     * @return json
     */
    public function index()
    {
        $param = input('param.');
        $model = new CompanyPrayerLazhuModel;
        try{
            $data = $model->findData($param['id'],'comid','standard,name,intro,days');
            if(empty($data)){
                $data = $this->moren;
            }else{
                $data['standard'] = unserialize($data['standard']);
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [record  记录]
     * @param
     * @author [C。]
     * @return json
     */
    public function record()
    {
        $param = input('param.');
        $model = new CompanyGongdengOrderModel;
        $user = $this->user;
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['c.mid'] = $user['id'];
            $map['c.comid'] = $param['id'];
            $map['c.status'] = 1;
            $map['c.type'] = 5;
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size,'c.gongdeng,c.day,c.total_price,c.content,c.create_time,c.bless');
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [donornum  捐款人数]
     * @param
     * @author [C。]
     * @return json
     */
    public function donornum()
    {
        $param = input('param.');
        $model = new CompanyPrayerLazhuModel;
        $cmodel = new CompanyGongdengOrderModel;
        try{
            $data = $model->findData($param['id'],'comid','donor_num');
            if(empty($data)){
                $data['donor_num'] = 0;
            }
            $data['really'] = $cmodel->getAllCount(['type'=>5,'status'=>1,'comid'=>$param['id']]);
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }

    /**
     * [lazhulist  获取燃烧中和已熄灭蜡烛]
     * @param type 1燃烧中 2已熄灭
     * @return json
     * @author [C。]
     */
    public function lazhulist()
    {
        $param = input('param.');
        $model = new CompanyPrayerLazhuModel;
        $this->getPageAndSize($param);
        $user = $this->user;
        try{
            if($param['type'] == 1){
                $code = '>';
            }else{
                $code = '<';
            }
            $countsql = 'select count( * ) as count from app_company_gongdeng_order where type = 5 and status = 1 and mid = '.$user['id'].' and comid = '.$param['id'].' and (paytime + day*24*3600) '.$code. time();
            $count = Db::query($countsql);
            $allpage = intval(ceil($count[0]['count'] / $this->size));//总页面
            $sql = 'select id,gongdeng,total_price,unit_price,content,day,create_time,paytime,states,bless,bless_img from app_company_gongdeng_order where type = 5 and status = 1 and mid = '.$user['id'].' and comid = '.$param['id'].' and (paytime + day*24*3600) '.$code. time().' order by paytime desc limit '.$this->from . ',' . $this->size;
            $lists = Db::query($sql);
            $companygongdeng = $model->findData($param['id'],'comid','standard');
            if(empty($companygongdeng)){
                $gongdeng = $this->moren;
            }else{
                $gongdeng = unserialize($companygongdeng['standard']);
            }
            $last_names = array_column($gongdeng, 'name');
            foreach($lists as &$v){
                if($param['type'] == 1){
                    $v['residue'] = $v['paytime'] + $v['day']*24*3600 - time();
                    $v['residue'] = time2string($v['residue']);
                }else{
                    $v['residue'] = '已灭';
                }
                $v['paytime'] = date('Y-m-d',$v['paytime']);
                $key = array_search($v['gongdeng'], $last_names);
                $v['img'] = $gongdeng[$key]['img'];
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, 'error' . $e->getMessage());
        }
        return admin_json(1, 'success', [
            'count' => $count[0]['count'],
            'allpage' => $allpage,
            'lists' => $lists,
        ]);
    }
    /**
     * [edit  编辑]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function edit()
    {
        $model = new CompanyPrayerLazhuModel;
        $param = input('param.');
        try{
            $gongdeng = $model->findData($param['comid'],'comid','comid');
            if(empty($gongdeng)){
                $param['standard'] = serialize($param['standard']);
                $model->addData($param);
            }else{
                $param['standard'] = serialize($param['standard']);
                $model->editDataBy($param,['comid'=>$param['comid']]);
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '编辑成功');
    }
}